package com.weyoung.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.weyoung.app.dto.req.UserHelpAndFeedbackReqDTO;
import com.weyoung.app.dto.req.UserLoginReqDTO;
import com.weyoung.app.dto.req.UserRegisterReqDTO;
import com.weyoung.app.dto.req.UserUpdateReqDTO;
import com.weyoung.app.dto.resp.UserDetailRespDTO;
import com.weyoung.app.dto.resp.UserLoginRespDTO;
import com.weyoung.app.service.HelpAndFeedbackService;
import com.weyoung.app.service.UserDetailService;
import com.weyoung.app.service.UserService;
import com.weyoung.common.consatant.exception.ClientException;
import com.weyoung.common.consatant.result.Result;
import com.weyoung.common.model.dto.TXOssSetDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * C端-用户接口管理
 */
@RestController
@RequestMapping("app/user")
@Tag(name = "C端-用户接口")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final UserDetailService userDetailService;
    private final HelpAndFeedbackService helpAndFeedbackService;

     /**
     * 登录
     *
     * @param requestParam 用户名+密码
     * @return 成功
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Result.success(userService.login(requestParam));
    }

    /**
     * 注册
     *
     * @param requestParam 测试为用户名+密码，后面还加上 手机号+验证码
     * @return Void
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        String rePassword = requestParam.getRePassword();
        String password = requestParam.getPassword();
        if (rePassword==null) {
            throw new ClientException("密码不能为空");
        }
        if (!rePassword.equals(password)) {
            throw new ClientException("两次密码不一致");
        }
        return Result.success(userService.register(requestParam));
    }

    /**
     * 退出登录
     *
     * @return void
     */
    @PostMapping("logout")
    @Operation(summary = "退出登录")
    public Result<Void> logout() {
        StpUtil.logout();
        return Result.success();
    }

    /**
     * 查询个人详细信息
     *
     * @return 用户信息
     */
    @GetMapping("detail")
    public Result<UserDetailRespDTO> userDetail(){
        return Result.success(userDetailService.userDetail());
    }

    /**
     * 更新个人详细信息,这里不能添加父母
     *
     * @param requestParam 更新信息
     * @return 更新结果
     */
    @PutMapping("detail")
    @Operation(summary = "更新个人详细信息")
    public Result<Void> updateUserDetail(@RequestBody UserUpdateReqDTO requestParam) {
        userDetailService.updateUserDetail(requestParam);
        return Result.success();
    }

    /**
     * 获取家族邀请码
     *
     * @return 邀请码
     */
    @GetMapping("invite")
    public Result<String> invite(){
        return Result.success(userService.invite());
    }

      /**
     * 根据邀请码加入家族
     *
     * @param inviteCode 邀请码
     * @return 成功或失败信息
     */
    @PostMapping("join-family/{inviteCode}")
    @Operation(summary = "根据邀请码加入家族")
    public Result<Void> joinFamilyByInviteCode(@PathVariable("inviteCode") String inviteCode) {
        userService.joinFamilyByInviteCode(inviteCode);
        return Result.success();
    }

    /**
     * 修改头像
     *
     * @param file 图片
     * @return 无
     * @throws Exception 1
     */
    @Operation(summary = "上传文件")
    @PostMapping("/updateUserPic")
    public Result<Void> updateUserPic(@RequestPart("file") MultipartFile file) throws Exception {
        userService.updateUserPic(file);
        return Result.success();
    }

    /**
     * 用户反馈
     *
     * @param requestParam 反馈信息
     * @return
     */
    @Operation(summary = "用户反馈")
    @PostMapping("UserHelpAndFeedback")
    public Result<Void> UserHelpAndFeedback(@RequestBody UserHelpAndFeedbackReqDTO requestParam){
       helpAndFeedbackService.addHelpAndFeedback(requestParam);
        return Result.success();
    }


}